package fractal;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import entity.Position;
import entity.map.FieldEntity;
import entity.map.FieldEntityMap;
import entity.transformer.FieldEntityMapTransformer;

import json.JSONException;

public class GenerateMapFile
{
	public static int LENGTH_MAP = 20;

	public static void generateMapFile(TerrainMap pMap, String pPath)
			throws IOException, JSONException
	{
		for (int i = 0; i < pMap.getMap().length; i++)
		{
			for (int j = 0; j < pMap.getMap()[0].length; j++)
			{
				Terrain terrain = pMap.getMap()[i][j];
				String pFileName = pPath + "\\" + i + "_" + j + ".mymap";
				PrintWriter file = new PrintWriter(new BufferedWriter(
						new FileWriter(pFileName)));
				generateMapFile(terrain, file);
				file.close();
			}
		}
	}

	public static void generateMapFile(Terrain pTerrain, PrintWriter pFile)
			throws JSONException
	{
		FieldEntityMap map = new FieldEntityMap(LENGTH_MAP, LENGTH_MAP);
		if (pTerrain.getForest())
		{
			// pFile.println("foret");
		}
		if (TerrainType.DESERT.equals(pTerrain.getTerrainType()))
		{
			// System.out.println("dd");
		}
		// pFile.println("eau : " + pTerrain.getWater());
		// pFile.println("hauteur : " + pTerrain.getHauteur());
		// pFile.println("temps : " + pTerrain.getTemperature());
		// pFile.println("##");
		//
		for (int i = 0; i < LENGTH_MAP; i++)
		{
			for (int j = 0; j < LENGTH_MAP; j++)
			{
				FieldEntity fieldEntity = new FieldEntity(new Position(i, j));
				fieldEntity.setFieldCode("AADD");
				map.setFieldEntity(fieldEntity);
			}
		}
		pFile.print(new FieldEntityMapTransformer().getJSON(map));

	}

	public static void main(String[] args) throws IOException, JSONException
	{

		TerrainMap map = new TerrainMap(65);
		map.generateWorld();
		// new GenerateurKingdom().createKingdom(map);
		generateMapFile(map, "S:\\perso\\project\\map");
	}

}
